package cn.sunshine.org.controller;

import cn.sunshine.basic.annotation.PreAuthorize;
import cn.sunshine.basic.utils.AjaxResult;
import cn.sunshine.basic.utils.PageList;
import cn.sunshine.org.domain.Employee;
import cn.sunshine.org.query.EmployeeQuery;
import cn.sunshine.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /*
     * @Title: loadById
     * @Description: 根据id查询
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/23 17:42
     * @Parameters: [id]
     * @Return cn.sunshine.org.domain.Employee
     */
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据Id查询", sn = "emp:loadById")
    public Employee loadById(@PathVariable("id") Long id) {

        return employeeService.loadById(id);
    }

    @GetMapping
    @PreAuthorize(name = "查询所有", sn = "emp:loadAll")
    public List<Employee> loadAll() {

        return employeeService.loadAll();
    }

    /*
     * @Title: queryData
     * @Description: 高级查询查询所有
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/23 17:43
     * @Parameters: [employeeQuery]
     * @Return cn.sunshine.basic.utils.PageList<cn.sunshine.org.domain.Employee>
     */
    @PostMapping
    @PreAuthorize(name = "高级查询查询所有", sn = "emp:queryData")

    public PageList<Employee> queryData(@RequestBody EmployeeQuery employeeQuery) {
        System.out.println(employeeQuery);
        return employeeService.queryData(employeeQuery);
    }

    /*
     * @Title: remove
     * @Description: 根据id删除数据
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/23 17:58
     * @Parameters: [id]
     * @Return cn.sunshine.basic.utils.AjaxResult
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "根据Id查询", sn = "emp:remove")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            employeeService.remove(id);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请重试");
        }
    }

    /*
     * @Title: addOrUpdate
     * @Description: 增加与修改
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/23 18:17
     * @Parameters: [employee]
     * @Return cn.sunshine.basic.utils.AjaxResult
     */
    @PutMapping
    @PreAuthorize(name = "新增或者修改员工", sn = "emp:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.save(employee);

            } else {
                employeeService.update(employee);

            }
            return AjaxResult.getAjaxResult();

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请重试");
        }
    }

    /*
     * @Title: batchEmployee
     * @Description: 批量删除员工数据
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/25 0:25
     * @Parameters: [ids]
     * @Return cn.sunshine.basic.utils.AjaxResult
     */
    @PatchMapping
    @PreAuthorize(name = "批量删除员工数据", sn = "emp:batchRemove")
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {// 调用删除方法，根据id删除
            employeeService.batchRemove(ids);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请重试");

        }
    }
}
